package com.future.mayi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.ToastUtil;
import com.future.mayi.R;
import com.future.mayi.model.CateModel;
import com.future.mayi.utils.PriceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 周大虎 on 2017/5/25.
 */

public class AppraisalAdapter extends BaseRecyclerViewAdapter<CateModel> {


    private AppraisalAdapterListener listener;

    public interface AppraisalAdapterListener {
        void onChange();
    }

    public AppraisalAdapter(Context context) {
        super(context, null);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(getView(parent, R.layout.layout_item_appraisal));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        ViewHolder holder = (ViewHolder) viewHolder;
        final CateModel model = getItem(position);

        loadImg(holder.ivIcon, model.getCate_img());
        setText(holder.tvName, model.getCate_name());


        if (model.getNum() > 0) {
            holder.llNum.setVisibility(View.VISIBLE);
            holder.ivAddOne.setVisibility(View.GONE);
        } else {
            holder.llNum.setVisibility(View.GONE);
            holder.ivAddOne.setVisibility(View.VISIBLE);
        }

        holder.tvNum.setText(String.valueOf(model.getNum()));
        holder.tvPrice.setText("¥"+PriceUtils.getPriceText(model.getPrice()));

        holder.ivAddOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                model.setNum(1);
                notifyDataSetChangeds();
            }
        });

        holder.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = model.getNum();
                if (num < 99) {
                    model.setNum(num + 1);
                    notifyDataSetChangeds();
                } else {
                    ToastUtil.getInstance().show(mContext, "超过最大添加数量");
                }
            }
        });

        holder.ivSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = model.getNum();
                if (num > 0) {
                    num = num - 1;
                    model.setNum(num);
                    notifyDataSetChangeds();
                }
            }
        });
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.iv_add_one)
        ImageView ivAddOne;
        @BindView(R.id.iv_sub)
        ImageView ivSub;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.iv_add)
        ImageView ivAdd;
        @BindView(R.id.ll_num)
        LinearLayout llNum;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void notifyDataSetChangeds() {
        if (listener != null)
            listener.onChange();
        super.notifyDataSetChanged();
    }

    public void setListener(AppraisalAdapterListener listener) {
        this.listener = listener;
    }


}
